package cn.tedu.egmvc9.controller;

import cn.tedu.egmvc9.mapper.NoticeMapper;
import cn.tedu.egmvc9.pojo.dto.NoticeAddParam;
import cn.tedu.egmvc9.pojo.dto.NoticeAddQuery;
import cn.tedu.egmvc9.pojo.dto.NoticeUpdateParam;
import cn.tedu.egmvc9.pojo.entity.Notice;
import cn.tedu.egmvc9.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egmvc9.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    NoticeMapper noticeMapper;
    @PostMapping("add")

    public String add(NoticeAddParam noticeAddParam) {
        System.out.println("noticeAddParam = " + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(100L);
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.insert(notice);
        return "新增资讯成功";
    }

    @GetMapping("list")

    public List<NoticeListVO> list(NoticeAddQuery noticeAddQuery){
        System.out.println("noticeAddQuery=" + noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS =noticeMapper.list(title,type);

        return noticeListVOS;
    }
    @GetMapping("detail")

    public NoticeDetailInfoVO getNoticeDetail(long id){

        NoticeDetailInfoVO noticeDetail =noticeMapper.getNoticeDetail(id);

        return noticeDetail;
    }

/*    @PostMapping("/v1/notice/del")
    @ResponseBody
    public String del(long id){

        Integer i = noticeMapper.del(id);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }*/
    @PostMapping("del")

    public String del(long id){
        Integer i= noticeMapper.del(id);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
    @PostMapping("update")

    public String update(NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(LocalDateTime.now());

        int i = noticeMapper.update(notice);
        if (i>0){
            return "修改成功";
        }else {
            return "修改失败";
        }

    }

}

